<?php
//рус
class App_Module_Admin_Module_Http extends Mozg_Module_Instance
{
    public function CheckAccessWithUserGranted(Mozg_Controller_Instance $ctrlObj, $basicActionName)
    {
		if ($this->GetCtrl('default')->IsRightRefferer())
		{
			return $ctrlObj->CheckAccessWithUserGranted($basicActionName);
		}
		return false;
    }

    public function Auth()
    {
    	$user = Mozg::User();

    	$usersMapper = Mozg_Mapper::Get('Users');

        $foundUser = false;

        if (Mozg::Request('http')->getPost('logme'))
        {
        	$userName = trim(Mozg::Request('http')->getPost('site_username'));
        	$password = Mozg::Request('http')->getPost('site_passwd');
        	$remember = (int)Mozg::Request('http')->getPost('site_rememberme');

            $foundUser = $usersMapper->GetOne(array('userName' => $userName));
			if ($foundUser
				&&
				(!Mozg::Lib('passhash')->CheckPassword($password, $foundUser->GetPasswordHash())
				|| $foundUser->GetBlocked())) $foundUser = false;

			if ($foundUser || Mozg_Mapper::Get('Session')->IsSession())
	        {
	            Mozg_Mapper::Get('Session')->Start((bool)$foundUser);

	            $sessionUser = Mozg_Mapper::Get('Session')->GetEntity('global', 'user');

	            if ($foundUser)
	            {
	            	if ($remember) Mozg_Mapper::Get('Session')->SetExpireDate(3600 * 24 * 30);

	            	$sessionUser->SetIsLogged(true);
	            	$sessionUser->SetId($foundUser->GetId());

	            	Mozg_Mapper::Get('Session')->Save($sessionUser);
	            }
	            else
	            {
	            	Mozg_Mapper::Get('Session')->Delete($sessionUser);
	            }

	        }

            if (!$foundUser)
            {
                $user->SetUserName($userName);
                $user->SetError(Mozg_Lang::_('INVALID_USER_PASSWORD'));

                $this->AfterFailedLogin();

                return $this;
            }

            $this->AfterSuccessLogin();
        }
        else if (!Mozg_Mapper::Get('Session')->IsSession())
        {
            return $this;
        }
        else if (Mozg::Request('http')->getPost('logout'))
        {
	        Mozg_Mapper::Get('Session')->Start();
	        Mozg_Mapper::Get('Session')->Stop();
	        $this->AfterSuccessLogout();

            return $this;
        }

        //запускаем сессию, если ещё не запущена
        Mozg_Mapper::Get('Session')->Start();
        $sessionUser = Mozg_Mapper::Get('Session')->GetEntity('global', 'user');

        if (!$sessionUser->GetIsLogged())
        {
            return $this;
        }

        if (!$foundUser)
        {
        	//if (0 === $sessionUser['user_id']) $foundUser = $usersMapper->GetSuperUser();
        	//else $foundUser = $usersMapper->GetById($sessionUser['user_id']);
        	$foundUser = $usersMapper->GetOne(array('id' => $sessionUser->GetId()));
        	if ($foundUser && $foundUser->GetBlocked()) return $this;
        }

        if ($foundUser)
        {
        	$foundUser->SetSessionId(Mozg_Mapper::Get('Session')->GetId());
        	Mozg::User($foundUser);
        }

    	return $this;
    }

    protected function AfterFailedLogin()
    {

    }

    protected function AfterSuccessLogin()
    {

    }

    protected function AfterSuccessLogout()
    {

    }
}